<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>画板</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <canvas id="canvas" width="100" height="100"></canvas>
    <div id="clear" onclick="clean()"></div>
    <script>
        function clean(){
            location.reload();
        }
    </script>
    <script>
        //设置画布为浏览器页面大小
        let canvas = document.getElementById("canvas");
        canvas.width = document.documentElement.clientWidth
        canvas.height = document.documentElement.clientHeight
    
        //设置画笔颜色和边框
        let ctx = canvas.getContext("2d");
        ctx.fillStyle = "black";
        ctx.strokeStyle = 'none'
        ctx.lineWidth = 8;
        ctx.lineCap = 'round';
    
        //设置画直线的代码
        function drawLine(x1, y1, x2, y2){
            ctx.beginPath();
            ctx.moveTo(x1, y1);
            ctx.lineTo(x2, y2);
            ctx.stroke();
        }
        //设置绘画状态，true表示点击，false表示松开
        let painting = false
        let last
        canvas.onmousedown = (e) => {
            painting = true
            last = [e.clientX, e.clientY]
        }
        canvas.onmouseup = () => {
            painting = false
        }
        
        //判断是否为触摸屏
        var isTouchDevice = 'ontouchstart' in
        document.documentElement;
        
        //判断触屏时是否绘画
        if (isTouchDevice) {
            //获取触摸屏第一个点位置
            canvas.ontouchstart = (e) => {
                let x = e.touches[0].clientX
                let y = e.touches[0].clientY
                last = [x, y] 
            }
            canvas.ontouchmove = (e) => {
                let x = e.touches[0].clientX
                let y = e.touches[0].clientY
                drawLine(last[0], last[1], x, y)
                last = [x, y]
            }
        }
            //判断电脑浏览页是否绘画
            canvas.onmousemove = (e) => {
            if(painting === true){
                drawLine(last[0], last[1], e.clientX, e.clientY)
                last = [e.clientX, e.clientY]
        }else{

        }
    }
    </script>
    <script src="main.js"></script>
</body>
</html>